package cn.com.dealme.domain.repository.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import cn.com.dealme.domain.model.Catagory;
import cn.com.dealme.domain.persistence.CatagoryPersistenceProxy;
import cn.com.dealme.domain.repository.ICatagoryRepository;

public class CatagoryRepositoryImpl implements ICatagoryRepository {
	
	private Session session;

	public void delete(Long id) {
		session.delete(session.get(CatagoryPersistenceProxy.class, id));
	}

	public void delete(Long[] ids) {
		for (Long id : ids) {
			session.delete(session.get(CatagoryPersistenceProxy.class, id));
		}
	}

	public Catagory find(Long id) {
		return (Catagory) session.get(CatagoryPersistenceProxy.class, id);
	}

	@SuppressWarnings("unchecked")
	public List<Catagory> listAll() {
		String entityName = session.getSessionFactory().getClassMetadata(
				CatagoryPersistenceProxy.class).getEntityName();
		return session.createQuery("from " + entityName).list();
	}
	
	@SuppressWarnings("unchecked")
	public List<Catagory> listAllTop() {
		Criteria dc = session.createCriteria(CatagoryPersistenceProxy.class);
		dc.add(Restrictions.eq("parent", null));
		return dc.list();
	}

	public Session getSession() {
		return session;
	}

	public void setSession(Session session) {
		this.session = session;
	}
}
